package com.yixun.qfbao.controller;

import com.alibaba.excel.EasyExcel;
import com.yixun.qfbao.api.service.AgencyReportService;
import com.yixun.qfbao.apidto.*;
import com.yixun.qfbao.common.model.PageDTO;
import com.yixun.qfbao.dto.*;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.exception.ResponseCode;
import com.yixun.qfbao.model.*;
import com.yixun.qfbao.service.CompanyService;
import com.yixun.qfbao.service.PartnerInfoService;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Controller
@ResponseBody
@RequestMapping("/api/agencyreport")
@Api(tags = {"分销报表"})
public class AgencyReportController {

    @Autowired
    LoginUtils loginUtils;
    @Autowired
    CompanyService companyService;
    @Autowired
    PartnerInfoService partnerInfoService;
    @Autowired
    AgencyReportService agencyReportService;

    @ApiOperation(notes = "获取分销部门", value = "获取分销部门")
    @PostMapping("/getAgencyDept")
    public BaseResponseDTO<GetAgencyDeptResDto> getAgencyDept(@RequestBody BaseRequestDTO<GetAgencyDeptReqDto> reqDto){
        BaseResponseDTO<GetAgencyDeptResDto> response = new BaseResponseDTO<>();
        if(StringUtils.isBlank(reqDto.getParams().getUserId()) && Objects.isNull(reqDto.getParams().getAgentCompany())){
            response.setResult(ResponseCode.PARAM_INVALID.getCode());
            response.setErrorMsg(String.format(ResponseCode.PARAM_INVALID.getMessageTemplate(), "未指定分销公司,"));
            return response;
        }

        // 分销公司默认不带公司参数,获取部门数据时,需要根据请求用户来指定分销公司
        if(Objects.isNull(reqDto.getParams().getAgentCompany()) && Objects.nonNull(reqDto.getParams().getUserId())){
            PartnerInfo partnerInfo = partnerInfoService.selectById(Long.valueOf(reqDto.getParams().getUserId()));
            reqDto.getParams().setAgentCompany(partnerInfo.getHigherid());
        }

        // 检验下公司是否分销公司
        Company company = companyService.selectById(reqDto.getParams().getAgentCompany());
        if(Objects.isNull(company) || 1 != company.getCompanyType()){
            response.setResult(ResponseCode.PARAM_INVALID.getCode());
            response.setErrorMsg("获取失败,非分销公司!");
            return response;
        }

        GetAgencyDeptResDto resDto = agencyReportService.getAgencyDept(reqDto.getParams());
        response.setResult(ResultEnum.SUCCESS.getNo());
        response.setErrorMsg(ResultEnum.SUCCESS.getDes());
        response.setResData(resDto);
        return response;
    }

    @ApiOperation(notes = "历史报备楼盘", value = "历史报备楼盘")
    @PostMapping("/getAgencyHisReportedPremises")
    public BaseResponseDTO<AgencyHisReportedPremisesResDto> getAgencyHisReportedPremises(@RequestBody BaseRequestDTO<AgencyHisReportedPremisesReqDto> reqDto){
        BaseResponseDTO<AgencyHisReportedPremisesResDto> response = new BaseResponseDTO<>();
        if(Objects.isNull(reqDto.getParams().getUserId())){
            response.setResult(ResponseCode.PARAM_INVALID.getCode());
            response.setErrorMsg(String.format(ResponseCode.PARAM_INVALID.getMessageTemplate(), "用户丢失,"));
            return response;
        }
        PartnerInfo partnerInfo = loginUtils.getUser(reqDto.getParams().getUserId());
        if(Objects.isNull(partnerInfo)){
            partnerInfo = partnerInfoService.selectById(Long.valueOf(reqDto.getParams().getUserId()));
        }
        reqDto.getParams().setStaffCompanyId(partnerInfo.getHigherid());
        AgencyHisReportedPremisesResDto resDto = agencyReportService.getAgencyHisReportedPremises(reqDto.getParams());
        response.setResult(ResultEnum.SUCCESS.getNo());
        response.setErrorMsg(ResultEnum.SUCCESS.getDes());
        response.setResData(resDto);
        return response;
    }


    @ApiOperation(notes = "楼盘销售情况", value="楼盘销售情况")
    @PostMapping("/getAgencyPremisesSalesReport")
    public BaseResponseDTO<AgencyPremisesSalesReportResDto> getAgencyPremisesSalesReport(@RequestBody BaseRequestDTO<AgencyPremisesSalesReportReqDto> reqDto) throws ParseException {
        BaseResponseDTO<AgencyPremisesSalesReportResDto> response = new BaseResponseDTO<>();
        if(Objects.isNull(reqDto.getParams().getUserId())){
            response.setResult(ResponseCode.PARAM_INVALID.getCode());
            response.setErrorMsg(String.format(ResponseCode.PARAM_INVALID.getMessageTemplate(), "用户丢失,"));
            return response;
        }
        PartnerInfo partnerInfo = loginUtils.getUser(reqDto.getParams().getUserId());
        if(Objects.isNull(partnerInfo)){
            partnerInfo = partnerInfoService.selectById(Long.valueOf(reqDto.getParams().getUserId()));
        }

        reqDto.getParams().setStaffCompanyId(partnerInfo.getHigherid());
        reqDto.getParams().setAccountType(partnerInfo.getAccountType());
        reqDto.getParams().setDeptId(partnerInfo.getDeptId());
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPageNum(reqDto.getParams().getPageNo());
        pageDTO.setPageSize(reqDto.getParams().getPageSize());
        AgencyPremisesSalesReportResDto resDto = agencyReportService.getAgencyPremisesSalesReport(reqDto.getParams(), pageDTO);
        response.setResult(ResultEnum.SUCCESS.getNo());
        response.setErrorMsg(ResultEnum.SUCCESS.getDes());
        response.setResData(resDto);
        return response;
    }

    @GetMapping(value = "exportAgencyPremisesSalesReport")
    @ApiOperation(value = "导出分销楼盘销售统计")
    public void exportAgencyPremisesSalesReport(AgencyPremisesSalesReportReqDto reqDto, HttpServletResponse response) throws IOException, ParseException {
        if(Objects.isNull(reqDto.getUserId())){
            return;
        }
        PartnerInfo partnerInfo = loginUtils.getUser(reqDto.getUserId());
        if(Objects.isNull(partnerInfo)){
            partnerInfo = partnerInfoService.selectById(Long.valueOf(reqDto.getUserId()));
        }
        reqDto.setStaffCompanyId(partnerInfo.getHigherid());
        reqDto.setAccountType(partnerInfo.getAccountType());
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPageNum(PageDTO.DEFAULT_PAGENUM);
        pageDTO.setPageSize(PageDTO.MAX_PAGESIZE);
        AgencyPremisesSalesReportResDto resDto = agencyReportService.getAgencyPremisesSalesReport(reqDto, pageDTO);
        List<AgencyPremisesSalesReport> reportList = resDto.getPageModel().getRows();
        List<AgencyPremisesSalesReportExport> exportList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(reportList)){
            for(AgencyPremisesSalesReport report: reportList){
                AgencyPremisesSalesReportExport exportData = new AgencyPremisesSalesReportExport();
                BeanUtils.copyProperties(report, exportData);
                if(Objects.nonNull(report.getCountTwo()) && Objects.nonNull(report.getCountSix())){
                    BigDecimal sign = new BigDecimal(report.getCountSix());
                    BigDecimal success = new BigDecimal(report.getCountTwo());
                    exportData.setSignRage(BigDecimal.ZERO);
                    if(success.compareTo(BigDecimal.ZERO) != 0){
                        exportData.setSignRage(sign.divide(success, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
                    }
                }
                exportList.add(exportData);
            }
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=楼盘销售情况.xlsx");
        EasyExcel.write(response.getOutputStream(), AgencyPremisesSalesReportExport.class).sheet("楼盘销售情况").doWrite(exportList);
    }

    @ApiOperation(notes = "汇总业绩", value="汇总业绩")
    @PostMapping("/getAgencyCompanyReport")
    public BaseResponseDTO<AgencyCompanyReportResDto> getAgencyCompanyReport(@RequestBody BaseRequestDTO<AgencyCompanyReportReqDto> reqDto) throws ParseException {
        BaseResponseDTO<AgencyCompanyReportResDto> response = new BaseResponseDTO<>();
        if(Objects.isNull(reqDto.getParams().getUserId())){
            response.setResult(ResponseCode.PARAM_INVALID.getCode());
            response.setErrorMsg(String.format(ResponseCode.PARAM_INVALID.getMessageTemplate(), "用户丢失,"));
            return response;
        }
        PartnerInfo partnerInfo = loginUtils.getUser(reqDto.getParams().getUserId());
        if(Objects.isNull(partnerInfo)){
            partnerInfo = partnerInfoService.selectById(Long.valueOf(reqDto.getParams().getUserId()));
        }
        reqDto.getParams().setStaffCompanyId(partnerInfo.getHigherid());
        reqDto.getParams().setAccountType(partnerInfo.getAccountType());
        reqDto.getParams().setDeptId(partnerInfo.getDeptId());
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPageNum(reqDto.getParams().getPageNo());
        pageDTO.setPageSize(reqDto.getParams().getPageSize());
        AgencyCompanyReportResDto resDto = agencyReportService.getAgencyCompanyReport(reqDto.getParams(), pageDTO);
        response.setResult(ResultEnum.SUCCESS.getNo());
        response.setErrorMsg(ResultEnum.SUCCESS.getDes());
        response.setResData(resDto);
        return response;
    }

    @GetMapping(value = "exportAgencyCompanyReport")
    @ApiOperation(value = "导出汇总业绩")
    public void exportAgencyCompanyReport(AgencyCompanyReportReqDto reqDto, HttpServletResponse response) throws IOException, ParseException {
        if(Objects.isNull(reqDto.getUserId())){
            return;
        }
        PartnerInfo partnerInfo = loginUtils.getUser(reqDto.getUserId());
        if(Objects.isNull(partnerInfo)){
            partnerInfo = partnerInfoService.selectById(Long.valueOf(reqDto.getUserId()));
        }
        reqDto.setStaffCompanyId(partnerInfo.getHigherid());
        reqDto.setAccountType(partnerInfo.getAccountType());
        reqDto.setDeptId(partnerInfo.getDeptId());
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPageNum(PageDTO.DEFAULT_PAGENUM);
        pageDTO.setPageSize(PageDTO.MAX_PAGESIZE);
        AgencyCompanyReportResDto resDto = agencyReportService.getAgencyCompanyReport(reqDto, pageDTO);
        List<AgencyCompanyReport> reportList = resDto.getPageModel().getRows();
        List<AgencyCompanyReportExport> exportList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(reportList)){
            for(AgencyCompanyReport report: reportList){
                AgencyCompanyReportExport exportData = new AgencyCompanyReportExport();
                BeanUtils.copyProperties(report, exportData);
                if(Objects.nonNull(report.getCountTwo()) && Objects.nonNull(report.getCountSix())){
                    BigDecimal sign = new BigDecimal(report.getCountSix());
                    BigDecimal success = new BigDecimal(report.getCountTwo());
                    exportData.setSignRage(BigDecimal.ZERO);
                    if(success.compareTo(BigDecimal.ZERO) != 0){
                        exportData.setSignRage(sign.divide(success, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
                    }
                }
                exportList.add(exportData);
            }
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=汇总业绩排名.xlsx");
        EasyExcel.write(response.getOutputStream(), AgencyCompanyReportExport.class).sheet("汇总业绩排名").doWrite(exportList);
    }

    @ApiOperation(notes = "合作机构部门业绩", value="合作机构部门业绩")
    @PostMapping("/getAgencyOrgReport")
    public BaseResponseDTO<AgencyOrgReportResDto> getAgencyOrgReport(@RequestBody BaseRequestDTO<AgencyOrgReportReqDto> reqDto) throws ParseException {
        BaseResponseDTO<AgencyOrgReportResDto> response = new BaseResponseDTO<>();
        if(Objects.isNull(reqDto.getParams().getUserId())){
            response.setResult(ResponseCode.PARAM_INVALID.getCode());
            response.setErrorMsg(String.format(ResponseCode.PARAM_INVALID.getMessageTemplate(), "用户丢失,"));
            return response;
        }
        PartnerInfo partnerInfo = loginUtils.getUser(reqDto.getParams().getUserId());
        if(Objects.isNull(partnerInfo)){
            partnerInfo = partnerInfoService.selectById(Long.valueOf(reqDto.getParams().getUserId()));
        }
        reqDto.getParams().setStaffCompanyId(partnerInfo.getHigherid());
        reqDto.getParams().setAccountType(partnerInfo.getAccountType());
        reqDto.getParams().setDeptId(partnerInfo.getDeptId());
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPageNum(reqDto.getParams().getPageNo());
        pageDTO.setPageSize(reqDto.getParams().getPageSize());
        AgencyOrgReportResDto resDto = agencyReportService.getAgencyOrgReport(reqDto.getParams(), pageDTO);
        response.setResult(ResultEnum.SUCCESS.getNo());
        response.setErrorMsg(ResultEnum.SUCCESS.getDes());
        response.setResData(resDto);
        return response;
    }

    @GetMapping(value = "exportAgencyOrgReport")
    @ApiOperation(value = "导出合作机构部门业绩")
    public void exportAgencyOrgReport(AgencyOrgReportReqDto reqDto, HttpServletResponse response) throws IOException, ParseException {
        if(Objects.isNull(reqDto.getUserId())){
            return;
        }
        PartnerInfo partnerInfo = loginUtils.getUser(reqDto.getUserId());
        if(Objects.isNull(partnerInfo)){
            partnerInfo = partnerInfoService.selectById(Long.valueOf(reqDto.getUserId()));
        }
        reqDto.setStaffCompanyId(partnerInfo.getHigherid());
        reqDto.setAccountType(partnerInfo.getAccountType());
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPageNum(PageDTO.DEFAULT_PAGENUM);
        pageDTO.setPageSize(PageDTO.MAX_PAGESIZE);
        AgencyOrgReportResDto resDto = agencyReportService.getAgencyOrgReport(reqDto, pageDTO);
        List<AgencyOrgReport> reportList = resDto.getPageModel().getRows();
        List<AgencyOrgReportExport> exportList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(reportList)){
            Map<Long, String> deptMap = reportList.stream()
                    .collect(Collectors.toMap(AgencyOrgReport::getDeptId, AgencyOrgReport::getDeptName, (oldValue, newValue) -> newValue));

            for(AgencyOrgReport report: reportList){
                AgencyOrgReportExport exportData = new AgencyOrgReportExport();
                BeanUtils.copyProperties(report, exportData);

                String deptName = "";
                if(StringUtils.isNotBlank(report.getPermissionCode())){
                    String [] pcArr = report.getPermissionCode().split("-");
                    for(int ii=0; ii<pcArr.length; ii++){
                        if(StringUtils.isBlank(deptMap.get(Long.valueOf(pcArr[ii])))){
                            continue;
                        }
                        deptName += deptMap.get(Long.valueOf(pcArr[ii]));
                        if(ii != (pcArr.length - 1) && StringUtils.isNotBlank(deptName)){
                            deptName += "/";
                        }
                    }
                }else{
                    deptName = report.getDeptName();
                }
                exportData.setDeptName(deptName);

                exportList.add(exportData);
            }
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=合作机构部门业绩.xlsx");
        EasyExcel.write(response.getOutputStream(), AgencyOrgReportExport.class).sheet("合作机构部门业绩").doWrite(exportList);
    }

    @ApiOperation(notes = "公司门店汇总业绩", value="公司门店汇总业绩")
    @PostMapping("/getAgencyStoreReport")
    public BaseResponseDTO<AgencyStoreReportResDto> getAgencyStoreReport(@RequestBody BaseRequestDTO<AgencyStoreReportReqDto> reqDto) throws ParseException {
        BaseResponseDTO<AgencyStoreReportResDto> response = new BaseResponseDTO<>();
        if(Objects.isNull(reqDto.getParams().getUserId())){
            response.setResult(ResponseCode.PARAM_INVALID.getCode());
            response.setErrorMsg(String.format(ResponseCode.PARAM_INVALID.getMessageTemplate(), "用户丢失,"));
            return response;
        }
        PartnerInfo partnerInfo = loginUtils.getUser(reqDto.getParams().getUserId());
        if(Objects.isNull(partnerInfo)){
            partnerInfo = partnerInfoService.selectById(Long.valueOf(reqDto.getParams().getUserId()));
        }
        reqDto.getParams().setStaffCompanyId(partnerInfo.getHigherid());
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPageNum(reqDto.getParams().getPageNo());
        pageDTO.setPageSize(reqDto.getParams().getPageSize());
        AgencyStoreReportResDto resDto = agencyReportService.getAgencyStoreReport(reqDto.getParams(), pageDTO);
        response.setResult(ResultEnum.SUCCESS.getNo());
        response.setErrorMsg(ResultEnum.SUCCESS.getDes());
        response.setResData(resDto);
        return response;
    }

    @GetMapping(value = "exportAgencyStoreReport")
    @ApiOperation(value = "导出公司门店汇总业绩")
    public void exportAgencyStoreReport(AgencyStoreReportReqDto reqDto, HttpServletResponse response) throws IOException, ParseException {
        if(Objects.isNull(reqDto.getUserId())){
            return;
        }
        PartnerInfo partnerInfo = loginUtils.getUser(reqDto.getUserId());
        if(Objects.isNull(partnerInfo)){
            partnerInfo = partnerInfoService.selectById(Long.valueOf(reqDto.getUserId()));
        }
        reqDto.setStaffCompanyId(partnerInfo.getHigherid());
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPageNum(PageDTO.DEFAULT_PAGENUM);
        pageDTO.setPageSize(PageDTO.MAX_PAGESIZE);
        AgencyStoreReportResDto resDto = agencyReportService.getAgencyStoreReport(reqDto, pageDTO);
        List<AgencyStoreReport> reportList = resDto.getPageModel().getRows();
        List<AgencyStoreReportExport> exportList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(reportList)){
            for(AgencyStoreReport report: reportList){
                AgencyStoreReportExport exportData = new AgencyStoreReportExport();
                BeanUtils.copyProperties(report, exportData);
                if(Objects.nonNull(report.getCountTwo()) && Objects.nonNull(report.getCountSix())){
                    BigDecimal sign = new BigDecimal(report.getCountSix());
                    BigDecimal success = new BigDecimal(report.getCountTwo());
                    exportData.setSignRage(BigDecimal.ZERO);
                    if(success.compareTo(BigDecimal.ZERO) != 0){
                        exportData.setSignRage(sign.divide(success, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
                    }
                }
                exportList.add(exportData);
            }
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=门店业绩排名.xlsx");
        EasyExcel.write(response.getOutputStream(), AgencyStoreReportExport.class).sheet("门店业绩排名").doWrite(exportList);
    }

    @ApiOperation(notes = "组别汇总业绩", value="组别汇总业绩")
    @PostMapping("/getAgencyGroupReport")
    public BaseResponseDTO<AgencyGroupReportResDto> getAgencyGroupReport(@RequestBody BaseRequestDTO<AgencyGroupReportReqDto> reqDto) throws ParseException {
        BaseResponseDTO<AgencyGroupReportResDto> response = new BaseResponseDTO<>();
        if(Objects.isNull(reqDto.getParams().getUserId())){
            response.setResult(ResponseCode.PARAM_INVALID.getCode());
            response.setErrorMsg(String.format(ResponseCode.PARAM_INVALID.getMessageTemplate(), "用户丢失,"));
            return response;
        }
        PartnerInfo partnerInfo = loginUtils.getUser(reqDto.getParams().getUserId());
        if(Objects.isNull(partnerInfo)){
            partnerInfo = partnerInfoService.selectById(Long.valueOf(reqDto.getParams().getUserId()));
        }
        reqDto.getParams().setStaffCompanyId(partnerInfo.getHigherid());
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPageNum(reqDto.getParams().getPageNo());
        pageDTO.setPageSize(reqDto.getParams().getPageSize());
        AgencyGroupReportResDto resDto = agencyReportService.getAgencyGroupReport(reqDto.getParams(), pageDTO);
        response.setResult(ResultEnum.SUCCESS.getNo());
        response.setErrorMsg(ResultEnum.SUCCESS.getDes());
        response.setResData(resDto);
        return response;
    }

    @GetMapping(value = "exportAgencyGroupReport")
    @ApiOperation(value = "导出组别汇总业绩")
    public void exportAgencyGroupReport(AgencyGroupReportReqDto reqDto, HttpServletResponse response) throws IOException, ParseException {
        if(Objects.isNull(reqDto.getUserId())){
            return;
        }
        PartnerInfo partnerInfo = loginUtils.getUser(reqDto.getUserId());
        if(Objects.isNull(partnerInfo)){
            partnerInfo = partnerInfoService.selectById(Long.valueOf(reqDto.getUserId()));
        }
        reqDto.setStaffCompanyId(partnerInfo.getHigherid());
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPageNum(PageDTO.DEFAULT_PAGENUM);
        pageDTO.setPageSize(PageDTO.MAX_PAGESIZE);
        AgencyGroupReportResDto resDto = agencyReportService.getAgencyGroupReport(reqDto, pageDTO);
        List<AgencyGroupReport> reportList = resDto.getPageModel().getRows();
        List<AgencyGroupReportExport> exportList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(reportList)){
            for(AgencyGroupReport report: reportList){
                AgencyGroupReportExport exportData = new AgencyGroupReportExport();
                BeanUtils.copyProperties(report, exportData);
                if(Objects.nonNull(report.getCountTwo()) && Objects.nonNull(report.getCountSix())){
                    BigDecimal sign = new BigDecimal(report.getCountSix());
                    BigDecimal success = new BigDecimal(report.getCountTwo());
                    exportData.setSignRage(BigDecimal.ZERO);
                    if(success.compareTo(BigDecimal.ZERO) != 0){
                        exportData.setSignRage(sign.divide(success, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
                    }
                }
                exportList.add(exportData);
            }
        }
        log.info("export size: {}", exportList.size());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=组别业绩排名.xlsx");
        EasyExcel.write(response.getOutputStream(), AgencyGroupReportExport.class).sheet("组别业绩排名").doWrite(exportList);
    }

    @ApiOperation(notes = "经纪人业绩", value="经纪人业绩")
    @PostMapping("/getAgencyAgentReport")
    public BaseResponseDTO<AgencyAgentReportResDto> getAgencyAgentReport(@RequestBody BaseRequestDTO<AgencyAgentReportReqDto> reqDto) throws ParseException {
        BaseResponseDTO<AgencyAgentReportResDto> response = new BaseResponseDTO<>();
        if(Objects.isNull(reqDto.getParams().getUserId())){
            response.setResult(ResponseCode.PARAM_INVALID.getCode());
            response.setErrorMsg(String.format(ResponseCode.PARAM_INVALID.getMessageTemplate(), "用户丢失,"));
            return response;
        }
        PartnerInfo partnerInfo = loginUtils.getUser(reqDto.getParams().getUserId());
        if(Objects.isNull(partnerInfo)){
            partnerInfo = partnerInfoService.selectById(Long.valueOf(reqDto.getParams().getUserId()));
        }
        reqDto.getParams().setStaffCompanyId(partnerInfo.getHigherid());
        reqDto.getParams().setAccountType(partnerInfo.getAccountType());
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPageNum(reqDto.getParams().getPageNo());
        pageDTO.setPageSize(reqDto.getParams().getPageSize());
        AgencyAgentReportResDto resDto = agencyReportService.getAgencyAgentReport(reqDto.getParams(), pageDTO);
        response.setResult(ResultEnum.SUCCESS.getNo());
        response.setErrorMsg(ResultEnum.SUCCESS.getDes());
        response.setResData(resDto);
        return response;
    }

    @GetMapping(value = "exportAgencyAgentReport")
    @ApiOperation(value = "导出经纪人业绩")
    public void exportAgencyAgentReport(AgencyAgentReportReqDto reqDto, HttpServletResponse response) throws IOException, ParseException {
        if(Objects.isNull(reqDto.getUserId())){
            return;
        }
        PartnerInfo partnerInfo = loginUtils.getUser(reqDto.getUserId());
        if(Objects.isNull(partnerInfo)){
            partnerInfo = partnerInfoService.selectById(Long.valueOf(reqDto.getUserId()));
        }
        reqDto.setStaffCompanyId(partnerInfo.getHigherid());
        reqDto.setAccountType(partnerInfo.getAccountType());
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPageNum(PageDTO.DEFAULT_PAGENUM);
        pageDTO.setPageSize(PageDTO.MAX_PAGESIZE);
        AgencyAgentReportResDto resDto = agencyReportService.getAgencyAgentReport(reqDto, pageDTO);
        List<AgencyAgentReport> reportList = resDto.getPageModel().getRows();
        List<AgencyAgentReportExport> exportList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(reportList)){
            Map<Long, String> deptMap = reportList.stream()
                    .collect(Collectors.toMap(AgencyAgentReport::getDeptId, AgencyAgentReport::getDeptName, (oldValue, newValue) -> newValue));

            for(AgencyAgentReport report: reportList){
                AgencyAgentReportExport exportData = new AgencyAgentReportExport();
                BeanUtils.copyProperties(report, exportData);
                String deptName = "";
                if(StringUtils.isNotBlank(report.getPermissionCode())){
                    String [] pcArr = report.getPermissionCode().split("-");
                    for(int ii=0; ii<pcArr.length; ii++){
                        if(StringUtils.isBlank(deptMap.get(Long.valueOf(pcArr[ii])))){
                            continue;
                        }
                        deptName += deptMap.get(Long.valueOf(pcArr[ii]));
                        if(ii != (pcArr.length - 1) && StringUtils.isNotBlank(deptName)){
                            deptName += "/";
                        }
                    }
                }else{
                    deptName = report.getDeptName();
                }
                exportData.setDeptName(deptName);
                exportList.add(exportData);
            }
        }
        log.info("export size: {}", exportList.size());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=经纪人业绩.xlsx");
        EasyExcel.write(response.getOutputStream(), AgencyAgentReportExport.class).sheet("经纪人业绩").doWrite(exportList);
    }

}
